let mysqlpool = require("../../model/connect");
const getChargeStationControler=async (request,response)=>{
    let {position} = request.query;
    position = JSON.parse(position);
    console.log("position",position);
    //取出表单charge_station和charge_station_info信息
    let sql_check = `select a.name,a.type,a.address,a.lng,a.lat,a.pname,a.cityname,a.adname,a.adname,a.tel,a.community,a.distance,b.charge_id,b.charge_station_all,b.charge_station_use,b.charge_price,b.charge_price_cheap,b.charge_station_status
    from charge_station as a,charge_station_info as b
    WHERE a.charge_id = b.charge_id`
    //获取距离目标位置最近10条充电站id
    let sql_check1 = `SELECT name,charge_id,
	(6371 * acos(cos(radians(${position.lat})) * cos(radians(lat)) * cos(radians(lng) - radians(${position.lng})) + sin(radians(${position.lat})) * sin(radians(lat)))) AS distance 
FROM charge_station ORDER BY distance LIMIT 0,10;`
    await mysqlpool.query(sql_check1,async (err,res)=>{
        console.log("res",res);
        res.forEach(async item => {
            await mysqlpool.query(`UPDATE charge_station SET distance=${item.distance} WHERE name='${item.name}';`)
        });
        let charge_ids = res.map(item => item.charge_id)
        charge_ids = charge_ids.toString();
        // console.log(charge_ids);
        await mysqlpool.query((sql_check+` AND a.charge_id in (${charge_ids})`), (err,res)=>{
            // console.log(res);
            response.send({chargeStation:res,total:res.length});
         })
    })
    
}
module.exports=getChargeStationControler